#include "widget.h"
GLuint VBO, VAO;

Triangle::Triangle()
{
}

Triangle::~Triangle()
{
    delete ourShader;
    core->glDeleteVertexArrays(1, &VAO);
    core->glDeleteBuffers(1, &VBO);
}

void Triangle::initializeGL()
{

    //着色器部分
    core = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_3_Core>();
    ourShader = new Shader(":/shaders/vertexshadersource.vert", ":/shaders/fragmentshadersource.frag");

    //VAO，VBO数据部分
    GLfloat vertices[] =
    {
        0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,  // Bottom Right
        -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,  // Bottom Left
        0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f   // Top
    };



    core->glGenVertexArrays(1, &VAO);//两个参数，第一个为需要创建的缓存数量。第二个为用于存储单一ID或多个ID的GLuint变量或数组的地址
    core->glGenBuffers(1, &VBO);
    core->glBindVertexArray(VAO);
    core->glBindBuffer(GL_ARRAY_BUFFER, VBO);

    core->glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    core->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void *)0);
    core->glEnableVertexAttribArray(0);
    core->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid *)(3 * sizeof(GLfloat)));
    core->glEnableVertexAttribArray(1);


    core->glBindBuffer(GL_ARRAY_BUFFER, 0);
    core->glBindVertexArray(0);

    core->glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

}

void Triangle::resizeGL(int w, int h)
{
    core->glViewport(0, 0, w, h);
}

void Triangle::paintGL()
{

    core->glClear(GL_COLOR_BUFFER_BIT);

    ourShader->use();

    ourShader->shaderProgram.setUniformValue(ourShader->shaderProgram.uniformLocation("posPlus"), 0.5f);
    core->glBindVertexArray(VAO);
    core->glDrawArrays(GL_TRIANGLES, 0, 3);

}
